#! python
import subprocess
import re


def parse_char(byte_str):
    char_vals = [int(x) for x in byte_str.split(',') if x != '0']
    bstr = str(bytes(char_vals),encoding='utf_8')
    return bstr

def walk_file(f, ):
    namelist = {}
    line = f.readline()
    while line:
        if re.match(r"\$L__info_string\d+:",line):
            name = line[:-2]
            buffer = ""
            line = f.readline().strip()
            while True:
                buffer += parse_char(line[4:])
                if line[-1] == '0':
                    sub = subprocess.run(['c++filt', buffer], stdout=subprocess.PIPE, text=True)
                    namelist[name] = sub.stdout.strip()
                    break
                line = f.readline().strip()
        
        line = f.readline()
    return namelist

if __name__ == '__main__':
    # s = parse_char('95,90,49,52,98,108,111,99,107,82,101,100,117,99,101,83,117,109,105')
    # os.system('c++filt '+s)
    with open('build/kernel_test.ptx','r') as f:
        for k,v in walk_file(f).items():
            print(f"{k} : {v}")
        f.close()


